package com.nj.liandi.gulimall.seckill.config;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.nj.liandi.common.exception.BizeCodeEnume;
import com.nj.liandi.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * description: 流量控制
 * date: 2021/11/12 11:44
 * author: maizj
 * version: 1.0
 */
@Configuration
public class SeckillSentinelConfig implements BlockExceptionHandler{

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        R error = R.error(BizeCodeEnume.TOO_MANY_REQUEST.getCode(), BizeCodeEnume.TOO_MANY_REQUEST.getMessage());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().write(JSON.toJSONString(error));
    }
}
